package com.ruoyi.system.service.impl;

import java.util.List;

import com.github.f4b6a3.uuid.UuidCreator;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TPartnerMapper;
import com.ruoyi.system.domain.TPartner;
import com.ruoyi.system.service.ITPartnerService;

/**
 * 合伙人Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Service
public class TPartnerServiceImpl implements ITPartnerService 
{
    @Autowired
    private TPartnerMapper tPartnerMapper;

    /**
     * 查询合伙人
     * 
     * @param id 合伙人主键
     * @return 合伙人
     */
    @Override
    public TPartner selectTPartnerById(String id)
    {
        return tPartnerMapper.selectTPartnerById(id);
    }

    /**
     * 查询合伙人列表
     * 
     * @param tPartner 合伙人
     * @return 合伙人
     */
    @Override
    public List<TPartner> selectTPartnerList(TPartner tPartner)
    {
        return tPartnerMapper.selectTPartnerList(tPartner);
    }
    
    /**
     * 验证账号
     * 
     * @param tPartner 合伙人
     * @return 结果
     */
    public String checkUsername(TPartner tPartner) {
    	//验证账号
    	TPartner checkPartner = tPartnerMapper.selectUsernameUsed(tPartner.getUsername());
        if(checkPartner != null && !checkPartner.getId().equals(tPartner.getId())) {
        	return "用户名已经被使用";
        }
    	return "";
    }

    /**
     * 新增合伙人
     * 
     * @param tPartner 合伙人
     * @return 结果
     */
    @Override
    public int insertTPartner(TPartner tPartner)
    {
        tPartner.setCreateTime(DateUtils.getNowDate());
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    	tPartner.setPassword(passwordEncoder.encode(tPartner.getPassword()));
    	
        String uniqueId = UuidCreator.getTimeOrderedEpoch().toString();
        tPartner.setId(uniqueId);
        
        if(tPartner.getCommissionRate() == null) {
        	tPartner.setCommissionRate((float)0);
        }
        tPartner.setCommissionRate(tPartner.getCommissionRate()/100);
        return tPartnerMapper.insertTPartner(tPartner);
    }

    /**
     * 修改合伙人
     * 
     * @param tPartner 合伙人
     * @return 结果
     */
    @Override
    public int updateTPartner(TPartner tPartner)
    {
        tPartner.setUpdateTime(DateUtils.getNowDate());
        
        if(tPartner.getPassword() != "" && tPartner.getPassword() != null) {
        	PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        	tPartner.setPassword(passwordEncoder.encode(tPartner.getPassword()));
        }else {
        	tPartner.setPassword(null);
        }
        if(tPartner.getCommissionRate() == null) {
        	tPartner.setCommissionRate((float)0);
        }
        tPartner.setCommissionRate(tPartner.getCommissionRate()/100);
        
        return tPartnerMapper.updateTPartner(tPartner);
    }

    /**
     * 批量删除合伙人
     * 
     * @param ids 需要删除的合伙人主键
     * @return 结果
     */
    @Override
    public int deleteTPartnerByIds(String[] ids)
    {
        return tPartnerMapper.deleteTPartnerByIds(ids);
    }

    /**
     * 删除合伙人信息
     * 
     * @param id 合伙人主键
     * @return 结果
     */
    @Override
    public int deleteTPartnerById(String id)
    {
        return tPartnerMapper.deleteTPartnerById(id);
    }
}
